<!--
 * @Date: 2025-12-01 16:26:30
 * @Description: 三维柱状图
 * @LastEditTime: 2025-12-02 10:42:36
-->
<template>
  <div class="charts" ref="chartRef"></div>
</template>

<script>
import echarts from 'echarts';
let myCharts = null;
export default {
  name: 'ThreeDBarChart',
  props: {
    list: {
      type: Array,
      default: () => [],
    },
  },
  mounted() {
    myCharts = echarts.init(this.$refs.chartRef);
  },
  watch: {
    list: {
      handler(newVal) {
        if (newVal && newVal.length > 0) {
          this.$nextTick(() => {
            myCharts.setOption(this.getOptions(newVal));
          });
        }
      },
      immediate: true,
      deep: true,
    },
  },
  methods: {
    getOptions(data) {
      let xData = [];
      let yData = [];

      if (data && data.length > 0) {
        data.forEach((item) => {
          xData.push(item.zoneName);
          yData.push(item.orderNum);
        });
      }

      // 绘制左侧面
      const CubeLeft = echarts.graphic.extendShape({
        shape: {
          x: 0,
          y: 0,
        },
        buildPath: function(ctx, shape) {
          // 会canvas的应该都能看得懂，shape是从custom传入的
          const xAxisPoint = shape.xAxisPoint;
          const c0 = [shape.x + 5, shape.y - 2];
          const c1 = [shape.x - 9, shape.y - 6];
          const c2 = [xAxisPoint[0] - 8, xAxisPoint[1] - 6];
          const c3 = [xAxisPoint[0] + 4, xAxisPoint[1] - 0];
          ctx
            .moveTo(c0[0], c0[1])
            .lineTo(c1[0], c1[1])
            .lineTo(c2[0], c2[1])
            .lineTo(c3[0], c3[1])
            .closePath();
        },
      });
      // 绘制右侧面
      const CubeRight = echarts.graphic.extendShape({
        shape: {
          x: 0,
          y: 0,
        },
        buildPath: function(ctx, shape) {
          const xAxisPoint = shape.xAxisPoint;
          const c1 = [shape.x + 4, shape.y];
          const c2 = [xAxisPoint[0] + 4, xAxisPoint[1]];
          const c3 = [xAxisPoint[0] + 12, xAxisPoint[1] - 5];
          const c4 = [shape.x + 12, shape.y - 5];
          ctx
            .moveTo(c1[0], c1[1])
            .lineTo(c2[0], c2[1])
            .lineTo(c3[0], c3[1])
            .lineTo(c4[0], c4[1])
            .closePath();
        },
      });
      // 绘制顶面
      const CubeTop = echarts.graphic.extendShape({
        shape: {
          x: 0,
          y: 0,
        },
        buildPath: function(ctx, shape) {
          const c1 = [shape.x + 6, shape.y - 1];
          const c2 = [shape.x + 12, shape.y - 5]; //右点
          const c3 = [shape.x - 5, shape.y - 9];
          const c4 = [shape.x - 9, shape.y - 6];
          ctx
            .moveTo(c1[0], c1[1])
            .lineTo(c2[0], c2[1])
            .lineTo(c3[0], c3[1])
            .lineTo(c4[0], c4[1])
            .closePath();
        },
      });

      // 注册三个面图形
      echarts.graphic.registerShape('CubeLeft', CubeLeft);
      echarts.graphic.registerShape('CubeRight', CubeRight);
      echarts.graphic.registerShape('CubeTop', CubeTop);

      const VALUE = yData;

      return {
        backgroundColor: 'transparent',
        title: {
          text: '单位：个',
          show: false,
          textStyle: {
            color: '#7ebaf2',
            fontWeight: '800',
            fontSize: 12,
          },
          left: '18px',
          top: '1%',
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow',
          },
          formatter: function(params, ticket, callback) {
            const item = params[1];
            return item.name + ' : ' + item.value;
          },
        },
        grid: {
          left: '3%',
          right: '1%',
          top: '8%',
          bottom: '-12%',
          containLabel: true,
        },
        xAxis: {
          type: 'category',
          data: xData,
          axisLine: {
            show: false,
            lineStyle: {
              color: '#7ebaf2',
            },
          },
          axisTick: {
            show: false,
            length: 9,
            alignWithLabel: true,
            lineStyle: {
              color: '#7DFFFD',
            },
          },
          axisLabel: {
            fontSize: 10,
            color: '#fff',
            rotate: 45, // 设置文字倾斜45度
            // interval: 0,  // 每个标签都显示
            margin: 40, // 设置标签与轴线的间距
            align: 'center',
            verticalAlign: 'bottom', // 控制文字垂直对齐方式
            overflow: 'truncate', // 设置文字超出时的处理方式
            ellipsis: '...', // 设置文字超出时的省略符号
          },
        },
        yAxis: {
          type: 'value',
          splitNumber: 4, // 设置纵轴分段数为 5
          min: 0,
          axisLine: {
            show: false,
            lineStyle: {
              color: '#7ebaf2',
            },
          },
          splitLine: {
            show: false,
          },
          splitArea: {
            show: false,
            areaStyle: {
              color: ['rgba(26,50,83,1)', 'rgba(30,57,92,1)'],
            },
          },
          axisTick: {
            show: false,
          },
          axisLabel: {
            fontSize: 12,
            color: '#fff',
          },
          boundaryGap: ['20%', '20%'],
        },
        series: [
          {
            type: 'custom',
            renderItem: (params, api) => {
              let cubeLeftStyle = '';
              let cubeRightStyle = '';
              let cubeTopStyle = '';
              if (params.dataIndex == 0) {
                cubeLeftStyle = new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  {
                    offset: 0,
                    color: 'rgba(199, 41, 63, 1)',
                  },
                  {
                    offset: 1,
                    color: 'rgba(255, 112, 56, 1)',
                  },
                ]);
                cubeRightStyle = new echarts.graphic.LinearGradient(
                  0,
                  0,
                  0,
                  1,
                  [
                    {
                      offset: 0,
                      color: 'rgba(199, 41, 63, 1)',
                    },
                    {
                      offset: 1,
                      color: 'rgba(255, 112, 56, 1)',
                    },
                  ]
                );
                cubeTopStyle = new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  {
                    offset: 0,
                    color: 'rgba(255, 112, 56, 1)',
                  },
                  {
                    offset: 1,
                    color: 'rgba(199, 41, 63, 1)',
                  },
                ]);
              } else if (params.dataIndex == 1) {
                cubeLeftStyle = new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  {
                    offset: 0,
                    color: 'rgba(199, 41, 63, 1)',
                  },
                  {
                    offset: 1,
                    color: 'rgba(255, 112, 56, 1)',
                  },
                ]);
                cubeRightStyle = new echarts.graphic.LinearGradient(
                  0,
                  0,
                  0,
                  1,
                  [
                    {
                      offset: 0,
                      color: 'rgba(199, 41, 63, 1)',
                    },
                    {
                      offset: 1,
                      color: 'rgba(255, 112, 56, 1)',
                    },
                  ]
                );
                cubeTopStyle = new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  {
                    offset: 0,
                    color: 'rgba(255, 112, 56, 1)',
                  },
                  {
                    offset: 1,
                    color: 'rgba(199, 41, 63, 1)',
                  },
                ]);
              } else if (params.dataIndex == 2) {
                cubeLeftStyle = new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  {
                    offset: 0,
                    color: 'rgba(199, 41, 63, 1)',
                  },
                  {
                    offset: 1,
                    color: 'rgba(255, 112, 56, 1)',
                  },
                ]);
                cubeRightStyle = new echarts.graphic.LinearGradient(
                  0,
                  0,
                  0,
                  1,
                  [
                    {
                      offset: 0,
                      color: 'rgba(199, 41, 63, 1)',
                    },
                    {
                      offset: 1,
                      color: 'rgba(255, 112, 56, 1)',
                    },
                  ]
                );
                cubeTopStyle = new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  {
                    offset: 0,
                    color: 'rgba(255, 112, 56, 1)',
                  },
                  {
                    offset: 1,
                    color: 'rgba(199, 41, 63, 1)',
                  },
                ]);
              } else if (params.dataIndex == 3) {
                cubeLeftStyle = new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  {
                    offset: 0,
                    color: 'rgba(199, 41, 63, 1)',
                  },
                  {
                    offset: 1,
                    color: 'rgba(255, 112, 56, 1)',
                  },
                ]);
                cubeRightStyle = new echarts.graphic.LinearGradient(
                  0,
                  0,
                  0,
                  1,
                  [
                    {
                      offset: 0,
                      color: 'rgba(199, 41, 63, 1)',
                    },
                    {
                      offset: 1,
                      color: 'rgba(255, 112, 56, 1)',
                    },
                  ]
                );
                cubeTopStyle = new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  {
                    offset: 0,
                    color: 'rgba(255, 112, 56, 1)',
                  },
                  {
                    offset: 1,
                    color: 'rgba(199, 41, 63, 1)',
                  },
                ]);
              }
              const location = api.coord([api.value(0), api.value(1)]);
              return {
                type: 'group',
                children: [
                  {
                    type: 'CubeLeft',
                    shape: {
                      api,
                      xValue: api.value(0),
                      yValue: api.value(1),
                      x: location[0],
                      y: location[1],
                      xAxisPoint: api.coord([api.value(0), 0]),
                    },
                    style: {
                      fill: cubeLeftStyle,
                    },
                  },
                  {
                    type: 'CubeRight',
                    shape: {
                      api,
                      xValue: api.value(0),
                      yValue: api.value(1),
                      x: location[0],
                      y: location[1],
                      xAxisPoint: api.coord([api.value(0), 0]),
                    },
                    style: {
                      fill: cubeRightStyle,
                    },
                  },
                  {
                    type: 'CubeTop',
                    shape: {
                      api,
                      xValue: api.value(0),
                      yValue: api.value(1),
                      x: location[0],
                      y: location[1],
                      xAxisPoint: api.coord([api.value(0), 0]),
                    },
                    style: {
                      fill: cubeTopStyle,
                    },
                  },
                ],
              };
            },
            data: VALUE,
          },
          {
            type: 'bar',
            label: {
              normal: {
                show: true,
                position: 'top',
                fontSize: 12,
                color: '#fff',
                offset: [2, -15],
              },
            },
            itemStyle: {
              color: 'transparent',
            },
            tooltip: {},
            data: VALUE,
          },
        ],
      };
    },
  },
};
</script>

<style scoped lang="scss">
.charts {
  width: 100%;
  height: 100%;
}
</style>
